/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 4/15/13
 * Time: 10:53 PM
 * To change this template use File | Settings | File Templates.
 */
package societyGames.xmlFragment
{
import flash.events.Event;

import mx.events.CollectionEvent;
import mx.events.PropertyChangeEvent;

public class XMLContainer extends XMLFragment
{
  public static const CHANGED:String = "CHANGED"; //When a child of this container changes.

  public function createChildXMLFragment(childClass:Class, xml:XML = null):XMLFragment
  {
    var childXMLFragment:XMLFragment = XMLFragment(new childClass());
    childXMLFragment.initialize(this, xml);
    childXMLFragment.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onChange, false, 0, true);
    return childXMLFragment;
  }

  public function createXMLFragmentCollection(collectionClass:Class, parentXML:XML, childClass:Class, childName:String):XMLFragmentCollection
  {
    var xmlFragmentCollection:XMLFragmentCollection = new collectionClass();
    xmlFragmentCollection.initialize(this, parentXML, childClass, childName);
    xmlFragmentCollection.addEventListener(CollectionEvent.COLLECTION_CHANGE, onChange, false, 0, true);
    return xmlFragmentCollection;
  }

  private function onChange(event:Event):void
  {
    dispatchEvent(new Event(CHANGED));
  }
}
}
